import { Injectable ,UnauthorizedException} from '@nestjs/common';
import { UserService } from '../user/user.service';
import * as md5 from 'md5';
import {  JwtService } from '@nestjs/jwt';
@Injectable()
export class AuthService {
  constructor(
    private readonly UserService: UserService,
    private readonly jwtService: JwtService,
    ) {}
  async login({ password, username }) {
    const user = await this.UserService.findByUsername(username);
    const md5Password = md5(password).toUpperCase();
    if(user.password!==md5Password){
      throw new UnauthorizedException('账号或者密码错误');
    }
    const payload={username:user.username,userid:user.id};
    return {
      token:await this.jwtService.signAsync(payload),
    }
  }
}
